﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.library.Helper;
using NHibernateVertica;
using VerticaAPI.model;
using com.nm.Attributes;
using Vertica.Data.VerticaClient;
using george.Web;
using com.nm.Cache;
using com.nm.Helper;
using com.nm.library;
using george.Helper;

namespace VerticaAPI
{
    [Description("字典信息")]
    public class DwGlobalVariableHandler : BaseHandler
    {
        public VerticaDataHelper Helper { get; set; } = new VerticaDataHelper();

        #region (ok)返回DataTable ReturnGlobalVariableChildrenList

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="DictID"></param>
        /// <returns></returns>
        public DataTable ReturnGlobalVariableChildrenList(string parentDictValue)
        {

            try
            {
                var sql = $@"SELECT dictName, dictValue, parentDictValue, isValid, Memo, LoadTime, isModify
FROM ndw.ndw_global_dictionary
where  CompanyID = '{UserInfo.UserCompanyId}' and  isValid = 0 and parentDictValue = '{parentDictValue}'
order by dictName";
                var cmd = Helper.GetSqlStringCommand(sql);
                return Helper.ExecuteDataTable(cmd);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回DataTable ReturnGlobalVariableParentList

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable ReturnGlobalVariableParentList()
        {

            try
            {
                var sql = $@"SELECT * FROM ndw.ndw_global_parent_dictionary 
where CompanyID = '{UserInfo.UserCompanyId}' and isValid=0 
order by ID";
                var cmd = Helper.GetSqlStringCommand(sql);
                return Helper.ExecuteDataTable(cmd);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回DataTable ReturnParentGlobalVariableList

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable ReturnParentGlobalVariableList()
        {
            try
            {
                var sql = $@"SELECT *
FROM ndw.ndw_global_parent_dictionary
where CompanyID = '{UserInfo.UserCompanyId}' and isValid=0 
order by ID";
                var cmd = Helper.GetSqlStringCommand(sql);
                return Helper.ExecuteDataTable(cmd);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回DataTable ReturnGlobalVariableList

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="dictValue"></param>
        /// <returns></returns>
        public IList<DwGlobalVariableModel.NeceModel> ReturnGlobalVariableList(string parentDictValue)
        {
            try
            {
                var sql = $@"SELECT *
FROM ndw.ndw_global_dictionary
where CompanyID = '{UserInfo.UserCompanyId}' and parentDictValue='{parentDictValue}' and isValid = 0
order by dictName";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<DwGlobalVariableModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回DataSet ReturnGlobalVariable

        /// <summary>
        /// 返回DataSet
        /// </summary>
        /// <param name="dictValue"></param>
        /// <returns></returns>
        public DataSet ReturnGlobalVariable(string dictName, string parentDictValue)
        {
            try
            {
                var sql = @"SELECT * FROM {0}
where CompanyID = '{1}' and  dictName='{2}' and isValid = 0";
                var con = @"ndw.ndw_global_dictionary";
                if (string.IsNullOrWhiteSpace(parentDictValue))
                {
                    con = @"ndw.ndw_global_parent_dictionary";
                }
                sql = string.Format(sql, con, UserInfo.UserCompanyId, dictName);
                var cmd = Helper.GetSqlStringCommand(sql);
                return Helper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回最大ID  ReturnMaxDictID

        /// <summary>
        /// 返回最大ID
        /// </summary>
        /// <returns></returns>
        public long ReturnMaxDictID()
        {
            var sql = $@"select max(ID) from ndw.ndw_global_parent_dictionary where CompanyID = '{UserInfo.UserCompanyId}'";
            var cmd = Helper.GetSqlStringCommand(sql);
            var id = Helper.ExecuteScalar(cmd);
            return string.IsNullOrWhiteSpace(id.ToString()) ? 0 : Convert.ToInt64(id);
        }

        #endregion

        #region (ok)返回DataTable ReturnAllGlobalVariable

        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable ReturnAllGlobalVariable(bool isParent)
        {
            try
            {
                var sql = @"SELECT * FROM {0} where CompanyID = '{1}' order by dictName";
                var con = @"ndw.ndw_global_dictionary";
                if (isParent)
                {
                    con = @"ndw.ndw_global_parent_dictionary";
                }
                sql = string.Format(sql, con, UserInfo.UserCompanyId);
                var cmd = Helper.GetSqlStringCommand(sql);
                return Helper.ExecuteDataTable(cmd);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)保存父字典 CreParentGlobalVariable

        /// <summary>
        /// 保存父字典
        /// </summary>
        /// <param name="param"></param>
        public void CreParentGlobalVariable(DwGlobalVariableModel.ParentModel param)
        {
            try
            {
                var sql = @"INSERT INTO ndw.ndw_global_parent_dictionary
(ExtrSource, CompanyID, dictName, dictValue, isValid, ID)
VALUES(@ExtrSource,@CompanyID,@dictName, @dictValue, @isValid, @ID)";
                var vpc = new VerticaParameter[]
                {
                    new VerticaParameter("@ExtrSource", UserInfo.UserExtrSource),
                    new VerticaParameter("@CompanyID", UserInfo.UserCompanyId),
                    new VerticaParameter("@dictName", param.dictName),
                    new VerticaParameter("@dictValue", param.dictValue),
                    new VerticaParameter("@isValid", VerticaType.Numeric, 0L),
                    new VerticaParameter("@ID",VerticaType.Numeric, param.ID),
                };
                var cmdList = new List<CommandInfo>()
                {
                    new CommandInfo()
                    {
                        CommandText = sql,
                        Parameters = vpc,
                    },
                };
                Helper.ExecuteSqlTran(cmdList);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)保存子字典 CreChildrenGlobalVariable

        /// <summary>
        /// 保存子字典
        /// </summary>
        /// <param name="param"></param>
        public void CreChildrenGlobalVariable(DwGlobalVariableModel.NeceModel param)
        {
            try
            {
                var sql = @"INSERT INTO ndw.ndw_global_dictionary
(ExtrSource,CompanyID,dictName, dictValue, parentDictValue, isValid, Memo, isModify)
VALUES(@ExtrSource,@CompanyID,@dictName, @dictValue, @parentDictValue, @isValid, @Memo, @isModify)";
                var vpc = new VerticaParameter[]
                {
                    new VerticaParameter("@ExtrSource", UserInfo.UserExtrSource),
                    new VerticaParameter("@CompanyID", UserInfo.UserCompanyId),
                    new VerticaParameter("@dictName", param.dictName),
                    new VerticaParameter("@dictValue", param.dictValue),
                    new VerticaParameter("@parentDictValue", param.parentDictValue),
                    new VerticaParameter("@isValid", VerticaType.Numeric, 0L),
                    new VerticaParameter("@Memo",param.Memo),
                    new VerticaParameter("@isModify",param.isModify),
                };
                var cmdList = new List<CommandInfo>()
                {
                    new CommandInfo()
                    {
                        CommandText = sql,
                        Parameters = vpc,
                    },
                };
                Helper.ExecuteSqlTran(cmdList);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)修改字典 ModGlobalVariable

        /// <summary>
        /// 修改字典
        /// </summary>
        /// <param name="param"></param>
        public void ModGlobalVariable(DwGlobalVariableModel.ModifyModel param)
        {
            try
            {
                var sql = $@"UPDATE ndw.ndw_global_dictionary
SET dictName=@dictName, dictValue=@dictValue, isValid=@isValid, Memo=@Memo, isModify=@isModify
where dictName=@oldDictName and dictValue=@oldDictValue and parentDictValue=@parentDictValue and CompanyID = '{UserInfo.UserCompanyId}'";
                var vpc = new VerticaParameter[]
                {
                    new VerticaParameter("@dictName", param.newDict.dictName),
                    new VerticaParameter("@dictValue", param.newDict.dictValue),
                    new VerticaParameter("@isValid", param.newDict.isValid),
                    new VerticaParameter("@Memo", param.newDict.Memo),
                    new VerticaParameter("@isModify", param.newDict.isModify),
                    new VerticaParameter("@oldDictName", param.oldDict.dictName),
                    new VerticaParameter("@oldDictValue", param.oldDict.dictValue),
                    new VerticaParameter("@parentDictValue", param.oldDict.parentDictValue),
                };
                var cmdList = new List<CommandInfo>()
                {
                    new CommandInfo()
                    {
                        CommandText = sql,
                        Parameters = vpc,
                    },
                };
                if (param.newDict.dictName == "周末系数" && param.oldDict.parentDictValue == "周末系数" && param.newDict.isValid == param.oldDict.isValid)
                {
                    cmdList.Add(new CommandInfo()
                    {
                        CommandText = $"update ndw.ndw_CalendarDays set SuppleRate={Convert.ToDecimal(param.newDict.dictValue)} " +
                                      $"where CompanyID = '{UserInfo.UserCompanyId}' and DayType='周末' and GregorianDay>='{DateTime.Now.AddDays(1):yyyy-MM-dd}'",
                    });
                }
                Helper.ExecuteSqlTran(cmdList);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }


        #endregion

        #region (ok)根据字典名称获取值 GetGlobalVariableBydictName

        /// <summary>
        /// 根据字典名称获取值
        /// </summary>
        /// <param name="dictName"></param>
        /// <returns></returns>
        public DataSet GetGlobalVariableBydictName(string dictName)
        {
            try
            {
                var sql = $"select * from ndw.ndw_global_dictionary " +
                          $"where  CompanyID = '{UserInfo.UserCompanyId}' and  dictName='{dictName}' and isValid = 0";
                var cmd = Helper.GetSqlStringCommand(sql);
                return Helper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回季节信息 ReturnOdsErpSdMatSeason

        /// <summary>
        /// 返回季节信息
        /// </summary>
        /// <returns></returns>
        /// writer:梁家健
        /// Create Date：2017-03-17
        public IList<OdsErpSdMatSeasonModel.NeceModel> ReturnOdsErpSdMatSeason()
        {
            try
            {
                var sql = $@"select * from nods.nods_erp_sd_mat_season where ExtrSource = '{UserInfo.UserExtrSource}'";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<OdsErpSdMatSeasonModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回类别信息 ReturnOdsErpSdMatKind

        /// <summary>
        /// 返回类别信息
        /// </summary>
        /// <returns></returns>
        /// writer:梁家健
        /// Create Date：2017-03-18
        public IList<OdsErpSdMatKindModel.NeceModel> ReturnOdsErpSdMatKind()
        {
            try
            {
                var sql = $"select * from nods.nods_erp_sd_mat_kind where ExtrSource = '{UserInfo.UserExtrSource}' ";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<OdsErpSdMatKindModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回系列信息 ReturnOdsErpSdMatSeries

        /// <summary>
        /// 返回系列信息
        /// </summary>
        /// <returns></returns>
        /// writer:梁家健
        /// Create Date：2017-03-18
        public IList<OdsErpSdMatSeriesModel.NeceModel> ReturnOdsErpSdMatSeries()
        {
            try
            {
                var sql = $@"select * from nods.nods_erp_sd_mat_series where ExtrSource = '{UserInfo.UserExtrSource}' ";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<OdsErpSdMatSeriesModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region (ok)返回小品类信息 ReturnOdsErpSDMatItem

        /// <summary>
        /// 返回小品类信息
        /// </summary>
        /// <returns></returns>
        /// writer:梁家健
        /// Create Date：2017-03-23
        public IList<OdsErpSDMatItemModel.NeceModel> ReturnOdsErpSDMatItem()
        {
            try
            {
                var sql = $"select * from nods.nods_erp_SD_Mat_Item  where ExtrSource = '{UserInfo.UserExtrSource}' ";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<OdsErpSDMatItemModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion
        #region (ok)获取店铺等级下拉列表 ReturnGetGlobalVariableListShop
        //ReturnGetGlobalVariableListShop
        public IList<DwGlobalVariableModel.ShopAndCustBaseInfo> ReturnGetGlobalVariableListShop(string InfoType)
        {
            try
            {
                var sql = $@" select InfoID, InfoName from nods.nods_erp_SD_Bas_ShopAndCustBaseInfo " +
                    $"where ExtrSource = '{UserInfo.UserExtrSource}' and InfoType = '{InfoType}'" +
                          $"Order by InfoID";

                //var sql = $"select * from nods.nods_erp_SD_Mat_Item  where ExtrSource = '{UserInfo.UserExtrSource}' ";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<DwGlobalVariableModel.ShopAndCustBaseInfo>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        #endregion

        #region 没SQL，不用改版的

        #region 新增字典信息 CreateParentGlobalVariable

        [JsonType]
        [MustLogin]
        [Description("新增字典信息")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult CreateParentGlobalVariable([ParamDescription("输入参数")]DwGlobalVariableModel.ParentModel param)
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                if (param == null) return Error("参数对象为空");
                if (string.IsNullOrWhiteSpace(param.dictName)) return Error("模块名称不能为空");
                if (string.IsNullOrWhiteSpace(param.dictValue)) return Error("模块值不能为空");
                var query = FindAllParentGlobalVariable();
                if (query.Any(t => t.dictName == param.dictName && t.isValid == 0))
                    return Error($"【{param.dictName}】已存在");
                if (query.Any(t => t.dictValue == param.dictValue && t.isValid == 0))
                    return Error($"【{param.dictValue}】已存在");
                param.ID = ReturnMaxDictID() + 1L;
                CreParentGlobalVariable(param);
                return Success(0);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 新增字典信息 CreateChildrenGlobalVariable

        [JsonType]
        [MustLogin]
        [Description("新增字典信息")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult CreateChildrenGlobalVariable([ParamDescription("输入参数")]DwGlobalVariableModel.NeceModel param)
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                if (param == null) return Error("参数对象为空");
                if (string.IsNullOrWhiteSpace(param.dictName)) return Error("模块名称不能为空");
                if (string.IsNullOrWhiteSpace(param.dictValue)) return Error("模块值不能为空");
                var query = ReturnGlobalVariableList(param.parentDictValue);
                if (query.Any(t => t.dictName == param.dictName && t.isValid == 0))
                    return Error($"【{param.dictName}】已存在");
                CreChildrenGlobalVariable(param);
                return Success(0);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 修改子字典信息 ModifyChildrenGlobalVariable

        [JsonType]
        [MustLogin]
        [Description("修改子字典信息")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult ModifyChildrenGlobalVariable([ParamDescription("输入参数")]DwGlobalVariableModel.ModifyModel param)
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                if (param == null) return Error("参数对象为空");
                if (param.oldDict == null) return Error("参数对象为空");
                if (param.newDict == null) return Error("参数对象为空");
                if (string.IsNullOrWhiteSpace(param.newDict.dictName)) return Error("模块名称不能为空");
                var query = ReturnGlobalVariableList(param.newDict.parentDictValue);
                if (param.oldDict.dictName != param.newDict.dictName && query.Any(t => t.dictName == param.newDict.dictName && t.isValid == 0 && t.dictName != param.oldDict.dictName))
                    return Error($"【{param.newDict.dictName}】已存在");
                //if (query.Any(t => t.dictValue == param.newParam.dictValue && t.isValid == 0) &&
                //    param.oldParam.dictValue != param.newParam.dictValue)
                //    return Error($"字典值【{param.newParam.dictValue}】已存在");
                ModGlobalVariable(param);
                return Success(0);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 系统数据字典树 GetGlobalVariableTreeList

        [MustLogin]
        [Description("系统数据字典树")]
        [ReturnType(typeof(IList<DwGlobalVariableModel.ShowModel>))]
        public ApiResult GetGlobalVariableTreeList([ParamDescription("是否刷新redis值,默认0不刷新")]int type=0)
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                var cacheKey = $"all-GlobalVariableTree-{UserInfo.UserCompanyId}-list";
                var result = RedisCache.GetAndDecodeJson<IList<DwGlobalVariableModel.ShowModel>>(cacheKey);
                if (result == null || type==1)
                {
                    var pdt = ReturnGlobalVariableParentList();
                    var parentList = ModelConvertHelper<DwGlobalVariableModel.ParentModel>.ConvertToModel(pdt).ToList();
                    result = parentList.Select(d => new DwGlobalVariableModel.ShowModel
                    {
                        ID = d.ID,
                        dictName = d.dictName,
                        dictValue = d.dictValue,
                        isValid = d.isValid,
                        children = GetGlobalVariableChildrenList(d.dictValue),
                    }).ToList();
                    RedisCache.MinString(cacheKey, result.ToJson(), 30);
                }
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 根据父值获取字典 GetGlobalVariableChildrenList

        /// <summary>
        /// 根据父值获取字典
        /// </summary>
        /// <param name="parentDictValue"></param>
        /// <returns></returns>
        public IList<DwGlobalVariableModel.NeceModel> GetGlobalVariableChildrenList(string parentDictValue)
        {
            try
            {
                var dt = ReturnGlobalVariableChildrenList(parentDictValue);
                return ModelConvertHelper<DwGlobalVariableModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region 系统数据父字典列表 GetParentGlobalVariableList

        [MustLogin]
        [Description("系统数据父字典列表")]
        [ReturnType(typeof(IList<DwGlobalVariableModel.ParentModel>))]
        public ApiResult GetParentGlobalVariableList()
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                var dt = ReturnParentGlobalVariableList();
                var result = ModelConvertHelper<DwGlobalVariableModel.ParentModel>.ConvertToModel(dt);
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 系统数据字典列表 GetGlobalVariableList

        [MustLogin]
        [Description("系统数据字典列表")]
        [ReturnType(typeof(IList<DwGlobalVariableModel.NeceModel>))]
        public ApiResult GetGlobalVariableList([ParamDescription("字典值")]string dictValue, [ParamDescription("是否刷新redis值,默认0不刷新")]int type=0)
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                var cacheKey = $"all-GlobalVariable-{dictValue}-{UserInfo.UserCompanyId}-list";
                var result = RedisCache.GetAndDecodeJson<IList<DwGlobalVariableModel.NeceModel>>(cacheKey);
                if (result == null || type==1)
                {
                    result = ReturnGlobalVariableList(dictValue);
                    RedisCache.MinString(cacheKey, result.ToJson(), 30);
                }
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 系统数据字典详情 GetGlobalVariable

        [MustLogin]
        [Description("系统数据字典详情")]
        [ReturnType(typeof(DwGlobalVariableModel.NeceModel))]
        public ApiResult GetGlobalVariable([ParamDescription("字典名称")]string dictName, [ParamDescription("父值")]string parentDictValue)
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                if (string.IsNullOrWhiteSpace(dictName)) return Error("字典为空");
                var ds = ReturnGlobalVariable(dictName, parentDictValue);
                var result = ModelConvertHelper<DwGlobalVariableModel.NeceModel>.DataSetToEntity(ds);
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 获取所有父字典 FindAllParentGlobalVariable

        /// <summary>
        /// 获取所有父字典
        /// </summary>
        /// <returns></returns>
        public IList<DwGlobalVariableModel.ParentModel> FindAllParentGlobalVariable()
        {
            try
            {
                var dt = ReturnAllGlobalVariable(true);
                var result = ModelConvertHelper<DwGlobalVariableModel.ParentModel>.ConvertToModel(dt);
                return result;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region 获取所有字典 FindAllChildrenGlobalVariable

        /// <summary>
        /// 获取所有字典
        /// </summary>
        /// <returns></returns>
        public IList<DwGlobalVariableModel.NeceModel> FindAllChildrenGlobalVariable()
        {
            try
            {
                var dt = ReturnAllGlobalVariable(false);
                var result = ModelConvertHelper<DwGlobalVariableModel.NeceModel>.ConvertToModel(dt);
                return result;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion

        #region 季节信息 GetOdsErpSdMatSeason

        /// writer:梁家健
        /// Create Date：2017-03-21
        [MustLogin]
        [Description("季节信息")]
        [ReturnType(typeof(IList<OdsErpSdMatSeasonModel.NeceModel>))]
        public ApiResult GetOdsErpSdMatSeason()
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                return Success(ReturnOdsErpSdMatSeason());
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 类别信息 GetOdsErpSdMatKind

        /// writer:梁家健
        /// Create Date：2017-03-21
        [MustLogin]
        [Description("类别信息")]
        [ReturnType(typeof(IList<OdsErpSdMatKindModel.NeceModel>))]
        public ApiResult GetOdsErpSdMatKind()
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                return Success(ReturnOdsErpSdMatKind());
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 系列信息 GetOdsErpSdMatSeries

        /// writer:梁家健
        /// Create Date：2017-03-21
        [MustLogin]
        [Description("系列信息")]
        [ReturnType(typeof(IList<OdsErpSdMatSeriesModel.NeceModel>))]
        public ApiResult GetOdsErpSdMatSeries()
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                return Success(ReturnOdsErpSdMatSeries());
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #region 小品类信息 GetOdsErpSDMatItem

        /// writer:梁家健
        /// Create Date：2017-03-23
        [MustLogin]
        [Description("小品类信息")]
        [ReturnType(typeof(IList<OdsErpSDMatItemModel.NeceModel>))]
        public ApiResult GetOdsErpSDMatItem()
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                return Success(ReturnOdsErpSDMatItem());
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion


        #region 获取店铺等级下拉列表
        /// writer:GavinGu
        /// Create Date：20180105
        [MustLogin]
        [Description("获取店铺等级下拉列表")]
        [ReturnType(typeof(IList<DwGlobalVariableModel.ShopAndCustBaseInfo>))]
        public ApiResult GetGlobalVariableListShop(string InfoType)
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                return Success(ReturnGetGlobalVariableListShop(InfoType));
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        #endregion

        #endregion

        /// writer:ljj
        /// Create Date：2017-08-08
        [MustLogin]
        [Description("性别信息")]
        [ReturnType(typeof(IList<OdsErpSDMatSexModel.NeceModel>))]
        public ApiResult GetOdsErpSDMatSex()
        {
            if (string.IsNullOrEmpty(UserInfo.UserCompanyId))
            {
                return Error("该用户不属于任何公司!");
            }
            if (string.IsNullOrEmpty(UserInfo.UserExtrSource))
            {
                return Error("该用户所在公司没有任何数据源!");
            }
            try
            {
                var list = ReturnOdsErpSDMatSex();
                return Success(list);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        /// <summary>
        /// 返回性别信息
        /// </summary>
        /// <returns></returns>
        /// writer:ljj
        /// Create Date：2017-08-08
        public IList<OdsErpSDMatSexModel.NeceModel> ReturnOdsErpSDMatSex()
        {
            try
            {
                var sql = $@"SELECT  ExtrSource ,
        SexID ,
        SexName
FROM    nods.nods_erp_SD_Mat_Sex
WHERE   ExtrSource = '{UserInfo.UserExtrSource}'
        AND AllowUsed = 1";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<OdsErpSDMatSexModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw(ex);
            }
        }

        /// writer:ljj
        /// Create Date：2017-08-08
        [MustLogin]
        [Description("货品信息")]
        [ReturnType(typeof(IList<OdsErpSDMatMaterialModel.NeceModel>))]
        public ApiResult GetOdsErpSDMatMaterial([ParamDescription("年份")] long YearNo = -1,
            [ParamDescription("季节")] string SeasonID = "", [ParamDescription("性别")] string SexID = "",
            [ParamDescription("类别")] string KindID = "", [ParamDescription("系列")] string SeriesID = "",
            [ParamDescription("商品层")] string MaterialProperty = "", [ParamDescription("品类")] string ModelID = "",
            [ParamDescription("小品类")] string ItemID = "", [ParamDescription("货号")] string MaterialIDs = "")
        {
            try
            {
                var materialIDStr = StringHelper.GetString(MaterialIDs);
                var list = ReturnOdsErpSDMatMaterial(YearNo == -1 ? (long?) null : YearNo, SeasonID, SexID, KindID,
                    SeriesID, MaterialProperty, ModelID,
                    ItemID, materialIDStr);
                return Success(list);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        /// <summary>
        /// 返回货品信息
        /// </summary>
        /// <returns></returns>
        /// writer:ljj
        /// Create Date：2017-08-08
        public IList<OdsErpSDMatMaterialModel.NeceModel> ReturnOdsErpSDMatMaterial(long? YearNo, string SeasonID, string SexID, string KindID, string SeriesID, string MaterialProperty, string ModelID, string ItemID, string materialIDStr)
        {
            try
            {
                var sql = $@"SELECT  ExtrSource ,
        CompanyID ,
        YearNo ,
        SeasonID ,
        KindID ,
        SeriesID ,
        ModelID ,
        ItemID ,
        MaterialProperty ,
        SexID ,
        MaterialID ,
        MaterialShortName
FROM    nods.nods_erp_SD_Mat_Material2
WHERE   ExtrSource = '{UserInfo.UserExtrSource}'
        AND CompanyID = '{UserInfo.UserCompanyId}'
        {(YearNo.HasValue ? $"AND YearNo = {YearNo.Value}" : "")}
        {(string.IsNullOrEmpty(SeasonID) ? "" : $"AND SeasonID = '{SeasonID}'")}
        {(string.IsNullOrEmpty(KindID) ? "" : $"AND KindID = '{KindID}'")}
        {(string.IsNullOrEmpty(SeriesID) ? "" : $"AND SeriesID = '{SeriesID}'")}
        {(string.IsNullOrEmpty(ModelID) ? "" : $"AND ModelID = '{ModelID}'")}
        {(string.IsNullOrEmpty(ItemID) ? "" : $"AND ItemID = '{ItemID}'")}
        {(string.IsNullOrEmpty(MaterialProperty) ? "" : $"AND MaterialProperty = '{MaterialProperty}'")}
        {(string.IsNullOrEmpty(SexID) ? "" : $"AND SexID = '{SexID}'")}
        {(string.IsNullOrEmpty(materialIDStr) ? "" : $"AND MaterialID IN {materialIDStr}")}
GROUP BY ExtrSource ,
        CompanyID ,
        YearNo ,
        SeasonID ,
        KindID ,
        SeriesID ,
        ModelID ,
        ItemID ,
        MaterialProperty ,
        SexID ,
        MaterialID ,
        MaterialShortName 
Order By MaterialShortName,MaterialID
";
                var cmd = Helper.GetSqlStringCommand(sql);
                var dt = Helper.ExecuteDataTable(cmd);
                return ModelConvertHelper<OdsErpSDMatMaterialModel.NeceModel>.ConvertToModel(dt);
            }
            catch (Exception ex)
            {
                throw(ex);
            }
        }
    }
}
